package org.groovyflow.spring.web;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InterceptorCache {
	public static final String AFTER = "after";
	public static final String BEFORE = "before";

	//Allow users to configure this?
	private Map before = new HashMap(5000);
	private Map after = new HashMap(5000);
	
	
	public void put(Object obj, String methodName, List interceptors, String type) {
		Map map = getMap(type);
		map.put(makeKey(obj, methodName), interceptors);
	}
	
	/**
	 * 
	 *  @return null if there's no entry in the cache, an empty
	 *  List if the cache entry shows no interceptors of the given type,
	 *  and otherwise a list of interceptor names.
	 */
	public List get(Object obj, String methodName, String type){
		Map map = getMap(type);
		return  (List) map.get(makeKey(obj, methodName));
	}
	
	public void clear(){
		before.clear();
		after.clear();
	}

	
	private Map getMap(String type){
		return AFTER.equals(type) ? after : before;
	}
	
	private String makeKey(Object obj, String methodName){
		StringBuffer buff = new StringBuffer();
		return buff.append(obj.getClass().getName()).append("XX").append(methodName).toString();
	}
}
